/*
ONEGAME
Copyright (C) 2009  Pablo Caballero

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package one.tool;

import java.util.ArrayList;
import one.map.ILayerMap;
import one.project.IProject;
import swing.tree.AbstractTreeModel;
import swing.tree.ITreeModelNode;

/**
 * Model for the tree tools
 * @author Pablo Caballero
 * @version 1.0 18-jun-2009
 */
public class ToolModel extends AbstractTreeModel {

	/**
	 * Project
	 */
	protected IProject project;

	/**
	 * Constructor
	 */
	public ToolModel() {
		setProject(null);
	}

	/**
	 * Get the value of project
	 * @return the value of project
	 */
	public IProject getProject() {
		return project;
	}

	/**
	 * Set the value of project
	 * @param project new value of project
	 */
	public void setProject(IProject project) {
		this.project = project;
	}

	public Object getRoot() {
		return getProject();
	}

	public Object getChild(Object parent, int index) {
		if (parent instanceof ITreeModelNode) {
			return ((ITreeModelNode) parent).getChild(index);
		} else {
			return null;
		}
	}

	public int getChildCount(Object parent) {
		if (parent instanceof ITreeModelNode) {
			return ((ITreeModelNode) parent).getChildCount();
		} else {
			return 0;
		}
	}

	public boolean isLeaf(Object node) {
		if (node instanceof ITreeModelNode) {
			return ((ITreeModelNode) node).isLeaf();
		} else {
			return true;
		}
	}

	/**
	 * Adds a new layer to the current project
	 */
	public void newLayer() {
		project.getSystemMaps().addLayer();
		updateStructure();
	}

	/**
	 * Deletes the layers
	 * @param layers the layers to be deleted
	 */
	public void deleteLayers(ArrayList<ILayerMap> layers) {
		if (project.getSystemMaps().deleteLayers(layers) > 0) {
			updateStructure();
		}
	}
}
